using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;


namespace dohoa2
{
    class Cparabol : Chinhve
    {
        int x1, y1, x2, y2;
        int a, x0, y0, xEnd, yEnd;
        Point dau, cuoi;
        public Cparabol(Bitmap hinh, Color mn, Point mot, Point hai)
        {
            dau = new Point(); dau = mot;
            cuoi = new Point(); cuoi = hai;
            anh = hinh; maunen = mn;

            x1 = dau.X; y1 = dau.Y;
            x2 = cuoi.X; y2 = cuoi.Y;
        }

        public Cparabol(Bitmap hinh, Color mn, int xx, int yy, int xx2, int yy2)
        {
            anh = hinh; maunen = mn;

            x1 = xx; y1 = yy;
            x2 = xx2; y2 = yy2;
            dau = new Point(); dau.X = x1; dau.Y = y1;
            cuoi = new Point(); cuoi.X = x2; cuoi.Y = y2;
        }

        public override void ve()
        {
            //Chi ve Parabol theo truc Oy
            {
                //truong hop x chay
                 x0 = (x1 + x2) / 2;
                 y0 = y1;
                 xEnd = Math.Abs(x2 - x1) / 2;
                 yEnd = Math.Abs(y2 - y1);
                 a = (yEnd == 0) ? 0 : (int)((long)xEnd * (long)xEnd / (long)yEnd);

                if (a == 0)//loai tru nhung truong hop bi tran so
                {
                   // DrawLine(pDC, x1, y1, x2, y2, color);
                    return;
                }

                int x = 0;
                int y = 0;
                int DauY = (y2 < y1) ? -1 : 1;
                int xt = a / 2 - 1;
                int p = 2 - a;

                int px = 6;
                int constant = -2 * a;
                Put2PixelX( x0, y0, x, DauY * y);
                while ((x < xt) && (x < xEnd))
                {
                    if (p >= 0)
                    {
                        p += constant;  //p=4*x+6-2*a
                        y++;
                    }
                    p += px;         //p=4*x+6
                    px += 4;
                    x++;
                    Put2PixelX( x0, y0, x, DauY * y);
                }
                //truong hop y chay
                p = 2 * a - 1;
                x = a / 2;
                y = a / 4;
                px = -4 * a - 8;
                constant = 4 * a;
                if (y < yEnd)
                    Put2PixelX( x0, y0, x, DauY * y);
                //chi hien thi trong phan muon nhin ma thoi
                //neu vuot ra ngoai tam nhin khong ve
                while (y < yEnd)
                {
                    if (p >= 0)
                    {
                        p += px;      //4*a-8*x-8;
                        px += -8;
                        x++;
                    }
                    p += constant;  //4*a
                    y++;
                    Put2PixelX( x0, y0, x, DauY * y);
                }

            }
        }


        private void Put2PixelX(int xc, int yc, int x, int y)
        {
            anh.SetPixel(xc + x, yc + y, mauve);
            anh.SetPixel(xc - x, yc + y, mauve);
        }


        public override void vechon() 
        {
            veonho((x1 + x2) / 2, y1);
            veonho(x1, y2);
            veonho(x2, y2);
        
        }

        public override  void tinhtien(Point mot, Point hai) 
        { int dx, dy;
           Color mauluu = new Color();
            mauluu = mauve;
            mauve = maunen;
            ve();

            if (duocchon == true) bochon();
           // MessageBox.Show("parabol"); 
            mauve = mauluu;
            dx = hai.X - mot.X;
            dy = hai.Y - mot.Y;
         
            if ((x1 + dx - kichthuoconho / 2 >= 0) && (x1 + dx + kichthuoconho / 2) <= anh.Width
                && (x2 + dx - kichthuoconho / 2) >= 0 && (x2 + dx + kichthuoconho / 2) <= anh.Width
                && (y1 + dy - kichthuoconho / 2 >= 0) && (y1 + dy + kichthuoconho / 2) <= anh.Height
                && (y2 + dy - kichthuoconho / 2) >= 0 && (y2 + dy + kichthuoconho / 2) <= anh.Height
            )
            {
                //dau.X += dx;
                x1 += dx;
//               cuoi.X += dx;
                x2 += dx;
                //dau.Y += dy;
                y1 += dy;
                //cuoi.Y += dy;
                y2 += dy;
            }
           
                ve();
                if (duocchon == true) vechon();
            
            
        }

        public override bool kiemtraduocchon(Point p)
        {
            long t = p.Y - a * (p.X * p.X);

            /*if (p.X > x1 && p.X < x2 && p.Y > y1 && p.Y < y2)
            {
                if (a > 0) if (t > 0) return true; else return false;
                if (a < 0) if (t < 0) return true; else return false;
            }*/
           // MessageBox.Show(t.ToString());
                return false;
        }
        
        }
    }
